package com.ynunicom.dc.dingdingcontractapp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ynunicom.dc.dingdingcontractapp.constant.ROLE_NAME;
import com.ynunicom.dc.dingdingcontractapp.entity.requestbodyentity.*;
import com.ynunicom.dc.dingdingcontractapp.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: jinye.Bai
 * @date: 2020/6/3 8:57
 */
@Service
public class RoleServiceImpl implements RoleService {

    private static final String CREATE_GROUP_URL ="https://oapi.dingtalk.com/role/add_role_group?access_token={access_token}";

    private static final String CREATE_ROLE_URL = "https://oapi.dingtalk.com/role/add_role?access_token={access_token}";

    private static final String GET_ROLE_GROUP_URL= "https://oapi.dingtalk.com/topapi/role/getrolegroup?access_token={access_token}";

    private static final String GET_ROLE_INFO_URL = "https://oapi.dingtalk.com/topapi/role/getrole?access_token={access_token}";

    private static final String GET_ROLE_LIST_URL = "https://oapi.dingtalk.com/topapi/role/list?access_token={access_token}";

    private static final String GET_STAFFLIST_BY_ROLE_URL = "https://oapi.dingtalk.com/topapi/role/simplelist?access_token={access_token}";

    private static final String UPDATE_ROLE_URL = "https://oapi.dingtalk.com/role/update_role?access_token={access_token}";

    private static final String DEL_ROLE_URL = "https://oapi.dingtalk.com/topapi/role/deleterole?access_token={access_token}";

    private static final String ADD_ROLES_FOR_STAFFS = "https://oapi.dingtalk.com/topapi/role/addrolesforemps?access_token={access_token}";

    private static final String DEL_ROLES_FOR_STAFFS= "https://oapi.dingtalk.com/topapi/role/removerolesforemps?access_token={access_token}";

    @Resource
    RestTemplate restTemplate;

    @Override
    public JSONObject getRoleList(TransBody<GetRoleListRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(GET_ROLE_LIST_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject getStaffListByRole(TransBody<GetStaffListByRoleRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(GET_STAFFLIST_BY_ROLE_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject getRoleGroup(TransBody<GetRoleGroupRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(GET_ROLE_GROUP_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject getRoleInfo(TransBody<DelRoleOrGetRoleInfoRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(GET_ROLE_INFO_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject createRole(TransBody<CreateRoleRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(CREATE_ROLE_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject updateRole(TransBody<UpdateRoleRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(UPDATE_ROLE_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject delRole(TransBody<DelRoleOrGetRoleInfoRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(DEL_ROLE_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject createRoleGroup(TransBody<CreateGroupRequestBody> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(CREATE_GROUP_URL,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject addRolesForStaffs(TransBody<DelOrAddRolesForStaffs> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(ADD_ROLES_FOR_STAFFS,transBody.getRequestBody(),JSONObject.class,urlVar);
    }

    @Override
    public JSONObject delRoleForStaffs(TransBody<DelOrAddRolesForStaffs> transBody) {
        Map<String,String> urlVar = new HashMap<>(1);
        urlVar.put("access_token",transBody.getAccess_token());
        return restTemplate.postForObject(DEL_ROLES_FOR_STAFFS,transBody.getRequestBody(),JSONObject.class,urlVar);
    }
}
